const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  mode: 'development', // 打包模式
  entry: {
    demo: './example/demo/index.js'
  },
  output: {
    path: path.resolve(__dirname, './dist-example'),
    filename: '[name]/index.js',
    clean: true
  },
  devServer: {
    open: ['demo/index.html'],
    client: {
      overlay: {
        errors: true,
        warnings: false
      }
    }
  },
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.less', '.scss']
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/, // 排除编译 node_modules
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(css|less)$/i,
        use: ['style-loader', 'css-loader', 'less-loader']
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'images/[name].[hash][ext]'
        }
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'fonts/[name]_[hash][ext]'
        }
      }
    ]
  },

  plugins: [
    new HtmlWebpackPlugin({
      title: 'react', // 用于生成的 HTML 文档的标题
      template: 'example/demo/index.html', // 指定模板
      filename: 'demo/index.html', // 定义打包后输出的文件名
      inject: 'body' // 将 script 放在 html 文件的什么位置
    })
  ]
}
